package com.walkline.screen;

import com.walkline.app.RadioAppConfig;
import com.walkline.radio.RadioPlayer;
import com.walkline.radio.dao.RadioChannel;
import com.walkline.util.Channels;
import com.walkline.util.Function;
import com.walkline.util.Enumerations.ChannelList;
import com.walkline.util.ui.ForegroundManager;
import com.walkline.util.ui.ListStyleButtonField;
import com.walkline.util.ui.ListStyleButtonSet;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;

public class ChannelListScreen extends MainScreen
{
	ForegroundManager _foreground = new ForegroundManager(0);
	ListStyleButtonSet _channelsSet = new ListStyleButtonSet();
	RadioPlayer _player = new RadioPlayer();
	Channels channels = Channels.getInstance();
	private RadioAppConfig _appConfig;

	public ChannelListScreen(int channelId, RadioPlayer player)
	{
		super (NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | NO_SYSTEM_MENU_ITEMS);

		setTitle(ChannelList.categories[channelId]);

		synchronized (Application.getEventLock())
		{
			_appConfig = new RadioAppConfig();
			_appConfig.initialize();
		}

		_player = player;

		ListStyleButtonField _item;
		RadioChannel channel;

		if (channelId == ChannelList.FAVORITES)
		{
			UiApplication.getUiApplication().invokeLater(new Runnable()
			{
				public void run() {onClose();}
			});
		} else {
			int emotion_count = _appConfig.getResisterStatus() ? channels.getCount(channelId) : 10;
			String[] channelArray = new String[emotion_count];

			for (int i=0; i<emotion_count; i++)
			{
				channelArray = channels.getEmotionsArray(channelId, i);
				channel = new RadioChannel(channelArray[0], channelArray[1]);

				_item = new ListStyleButtonField(channel);
				_item.setChangeListener(new FieldChangeListener()
				{
					public void fieldChanged(Field field, int context)
					{
						ListStyleButtonField item = (ListStyleButtonField) _channelsSet.getFieldWithFocus();
						_player.play(item.getRadioLink());
					}
				});
				_channelsSet.add(_item);
			}			
		}

		if (!_appConfig.getResisterStatus())
		{
			_item = new ListStyleButtonField("立即注册以获取全部电台列表");
			_item.setChangeListener(new FieldChangeListener()
			{
				public void fieldChanged(Field field, int context)
				{
					showRegisterScreen();
				}
			});
			_channelsSet.add(_item);
		}

		_foreground.add(_channelsSet);
		add(_foreground);
	}

	private void showRegisterScreen()
    {
    	RegisterScreen popupScreen = new RegisterScreen();
		UiApplication.getUiApplication().pushModalScreen(popupScreen);

		String code = popupScreen.getRegisterCode();

		if (popupScreen != null) {popupScreen = null;}
		if (!code.equals(""))
		{
			if (Function.getRegisterCode().equals(code))
			{
				_appConfig.setRegisterStatus(true);
				_appConfig.save();

				Function.errorDialog("注册成功，感谢你的支持！");
			} else {
				Function.errorDialog("注册码错误，请重新输入！");
			}
		}	
    }

	MenuItem menuRegister = new MenuItem("注册", 100, 10)
	{
    	public void run() {showRegisterScreen();}
    };

    MenuItem menuExit = new MenuItem("退出", 100, 20)
    {
    	public void run() {Function.showExitDialog();}
    };

	protected void makeMenu(Menu menu, int instance)
    {
		if (!_appConfig.getResisterStatus())
		{
			menu.add(menuRegister);
	    	menu.addSeparator();
		}

    	menu.add(menuExit);

    	super.makeMenu(menu, instance);
    }

	protected boolean keyChar(char key, int status, int time)
    {
		if (getFieldWithFocus() instanceof ForegroundManager)
		{
			switch (key)
			{
    			case Characters.SPACE:
    				_player.pause();
    				return true;
    			case Characters.LATIN_CAPITAL_LETTER_L:
    			case Characters.LATIN_SMALL_LETTER_L:
    				return true;
    			case Characters.LATIN_CAPITAL_LETTER_Q:
    			case Characters.LATIN_SMALL_LETTER_Q:
    				Function.showExitDialog();
    				return true;
			}
		}

    	return super.keyChar(key, status, time);
    }
}